import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Frame extends JFrame implements ActionListener,KeyListener {
        private boolean inframe = false;
        private boolean step = false;
        private int[] pos = {32,32};
        private int[] move =  {0,0};
        private int[] keys = {KeyEvent.VK_RIGHT,KeyEvent.VK_LEFT,KeyEvent.VK_DOWN,KeyEvent.VK_UP};
        private boolean[] press = {false,false,false,false};
        private String[] button = {">","<","v","^"};
        private boolean[] blocked = {false,false,false,false};
        private int mapsize = 18;
        private int[] map = {
                        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,
                        1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,
                        1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,
                        1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,
                        1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,
                        1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,
                        1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
                        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                        };
        private int[] block = {1,0,-1,0,0,1,0,-1};
        private int mCframe = 0;
        private int[] animation = {8,2,0,1};
        private int animationcounter = 0;
        private int facedir = 2;
        private boolean mirrored = false;
        
        Character main = new Character(0,0,0);
        maptiles baum = new maptiles(0,0);
        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JPanel panel = new JPanel(){
                public void paintComponent(Graphics g){
//                      g.clearRect(0, 0, this.getWidth(), this.getHeight());
//                      g.fillRect(pos[0],pos[1],16,16);
                		main.setCframe(mCframe);
                        main.drawCharacter(g, pos[0], pos[1]);
//                      for (int i=0;i<=160;i+=16){
//                              g.drawLine(i,0,i,160);
//                              g.drawLine(0,i,160,i);
//                      }
                        for (int i=0;i<map.length;i++){
                                if (map[i]!=0){
                                        int x=(i%mapsize)*16;
                                        int y=(i/mapsize)*16;
                                        //g.drawRect(x,y,16,16);
                                        //g.drawLine(x+8,y+8,x+8,y+8);
                                        baum.drawMaptile(g, x, y);
                                }
                        }
                }
        };
        Frame(){
                super("Mokepon");
                setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
                
                Timer timer = new Timer(20,this);
                timer.start();
                
                addKeyListener(this);
                
                panel.setLayout(null);
                label1.setBounds(100, 0, 100, 20);
                label2.setBounds(50, 140, 100, 20);
                panel.add(label1);
                panel.add(label2);
                this.add(panel);
        }
        
        public void moving(){
                if ((pos[0]%16==0) && (pos[1]%16==0)){
                        step = false;
                        animationcounter=0;
                        move[0]=0; move[1]=0;
                        for (int i=0;i<4;i++){
                                if (map[pos[0]/16+block[i*2]+(pos[1]/16+block[i*2+1])*mapsize]!=0){
                                        blocked[i]=true;
                                }else{
                                        blocked[i]=false;
                                }
                        }
                        
                }
                label1.setText("");
                for (int i=0;i<4;i++){
                        if (!step && press[i]){
                                if (blocked[i]){
                                        label1.setText("Boom!");
                                }else{
                                        step = true;
                                        move[i/2]=(int) Math.pow(-1,i);
                                }
                                facedir=i;
                        }
                }
        }
        
        public void updatepos() {
                for (int i=0;i<2;i++){
                        pos[i] += move[i];
                }
        }
        
        
        public void updateanimation(){
        	if (animationcounter==0){
        		mCframe=animation[facedir];
        	}
        	if (mirrored && facedir>=2 && mCframe<6){
        		mCframe+=6;
        	}
        	if(animationcounter==8){
        		mCframe+=3;
        	}else if(animationcounter==15){
        		mirrored=!mirrored;
        	}
        	animationcounter++;
        }
        
        
        public void actionPerformed(ActionEvent e) {
                if (!inframe){
                        inframe=!inframe;
                        
                        moving();
                        updatepos();
                        updateanimation();
                        
                        String s = "";
                        for (int i=0;i<4;i++){
                                if (press[i]){
                                        s+=button[i];
                                }else{
                                        s+="  ";
                                }
                        }
                        label2.setText(s);
                        
                        repaint();
                        
                        inframe=!inframe;
                }
        }

        public void keyPressed(KeyEvent e) {
                for (int i=0;i<4;i++){
                        if (e.getKeyCode()==keys[i]){
                                press[i]=true;
                        }
                }
        }
        
        public void keyReleased(KeyEvent e) {
                for (int i=0;i<4;i++){
                        if (e.getKeyCode()==keys[i]){
                                press[i]=false;
                        }
                }
        }
        
        public void keyTyped(KeyEvent e) {
                
        }
       /** public boolean getstep(){
        	return step;
        }
        public int[] getpos(){
        	return pos;
        }
        public int[] getmove(){
        	return move;
        }
        public boolean[] getpress(){
        	return press;
        }
        public boolean[] getblocked(){
        	return blocked;
        }
        public int getmapsize(){
        	return mapsize;
        }
        public int[] getmap(){
        	return map;
        }
        public int[] getblock(){
        	return block;
        }
        public void setstep(boolean pstep){
        	step = pstep;
        }
        public void setpos(int position, int ppos){
        	pos[position] = ppos;
        }
        public void setmove(int position, int pmove){
        	move[position] = pmove;
        }
        public void setpress(int position, boolean ppress){
        	press[position] = ppress;
        }
        public void setblocked(int position, boolean pblocked){
        	blocked[position] = pblocked;
        }
        public void setmapsize(int pmapsize){
        	mapsize = pmapsize;
        }
        public void setmap(int position, int pmap){
        	map[position] = pmap;
        }
        public void setblock(int position, int pblock){
        	block[position] = pblock;
        }*/
}
